package com.ruoyi.mall.controller;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

import com.ruoyi.mall.domain.MallMemberUser;
import com.ruoyi.mall.domain.MallTimeGoodsPool;
import com.ruoyi.mall.service.IMallMemberUserService;
import com.ruoyi.mall.service.IMallTimeGoodsPoolService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.mall.domain.MallTimeGoods;
import com.ruoyi.mall.service.IMallTimeGoodsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 抢购商品Controller
 *
 * @author ruoyi
 * @date 2022-02-25
 */
@RestController
@RequestMapping("/mall/mallTimeGoods")
public class MallTimeGoodsController extends BaseController
{
    @Autowired
    private IMallTimeGoodsService mallTimeGoodsService;
    @Autowired
    private IMallMemberUserService mallMemberUserService;
    @Autowired
    private IMallTimeGoodsPoolService mallTimeGoodsPoolService;

    /**
     * 查询抢购商品列表
     */
    @PreAuthorize("@ss.hasPermi('mall:mallTimeGoods:list')")
    @GetMapping("/list")
    public TableDataInfo list(MallTimeGoods mallTimeGoods)
    {
        startPage();
        List<MallTimeGoods> list = mallTimeGoodsService.selectMallTimeGoodsList(mallTimeGoods);
        return getDataTable(list);
    }

    /**
     * 导出抢购商品列表
     */
    @PreAuthorize("@ss.hasPermi('mall:mallTimeGoods:export')")
    @Log(title = "抢购商品", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(MallTimeGoods mallTimeGoods)
    {
        List<MallTimeGoods> list = mallTimeGoodsService.selectMallTimeGoodsList(mallTimeGoods);
        ExcelUtil<MallTimeGoods> util = new ExcelUtil<MallTimeGoods>(MallTimeGoods.class);
        return util.exportExcel(list, "抢购商品数据");
    }

    /**
     * 获取抢购商品详细信息
     */
    @PreAuthorize("@ss.hasPermi('mall:mallTimeGoods:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(mallTimeGoodsService.selectMallTimeGoodsById(id));
    }

    /**
     * 新增抢购商品
     */
    @PreAuthorize("@ss.hasPermi('mall:mallTimeGoods:add')")
    @Log(title = "抢购商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MallTimeGoods mallTimeGoods)
    {
        return toAjax(mallTimeGoodsService.insertMallTimeGoods(mallTimeGoods));
    }

    /**
     * 修改抢购商品
     */
    @PreAuthorize("@ss.hasPermi('mall:mallTimeGoods:edit')")
    @Log(title = "抢购商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MallTimeGoods mallTimeGoods)
    {
        return toAjax(mallTimeGoodsService.updateMallTimeGoods(mallTimeGoods));
    }

    /**
     * 删除抢购商品
     */
    @PreAuthorize("@ss.hasPermi('mall:mallTimeGoods:remove')")
    @Log(title = "抢购商品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(mallTimeGoodsService.deleteMallTimeGoodsByIds(ids));
    }

    /**
     * 修改抢购商品（商品池）
     */
    @Log(title = "修改抢购商品（商品池）", businessType = BusinessType.UPDATE)
    @PutMapping("/updateSetSplit")
    public AjaxResult updateSetSplit(@RequestBody MallTimeGoods mallTimeGoods)
    {
        if (mallTimeGoods.getOrderId() == null || mallTimeGoods.getOrderId() == 0) {
            AjaxResult.error("请选择商品池商品");
        }
        // 商品池商品
        MallTimeGoodsPool timeGoodsPool = mallTimeGoodsPoolService.selectMallTimeGoodsPoolById(mallTimeGoods.getOrderId());
        if (timeGoodsPool == null) {
            AjaxResult.error("商品池商品错误");
        }
        // 抢购商品
        MallTimeGoods timeGoods = mallTimeGoodsService.selectMallTimeGoodsById(mallTimeGoods.getId());
        if (mallTimeGoods.getPrice().compareTo(timeGoods.getPrice()) > -1
                || mallTimeGoods.getPrice().compareTo(new BigDecimal(0)) < 1) {
            AjaxResult.error("拆分价格不正确");
        }
        // 用户信息
        MallMemberUser memberUser = mallMemberUserService.selectMallMemberUserById(mallTimeGoods.getUserId());
        if (memberUser != null && timeGoodsPool != null) {
            BigDecimal timeGoodsPoolPrice = mallTimeGoods.getPrice();// 商品池商品价
            BigDecimal timeGoodsPrice = timeGoods.getPrice().subtract(mallTimeGoods.getPrice()).setScale(0, RoundingMode.HALF_UP);// 商品价
            // 商品池商品
            MallTimeGoods goodsPool = new MallTimeGoods();
            goodsPool.setUserId(memberUser.getId());
            goodsPool.setUserName(memberUser.getName()+"--"+memberUser.getPhone());
            goodsPool.setName(timeGoodsPool.getName());
            goodsPool.setPrice(timeGoodsPoolPrice);
            goodsPool.setRegionId(mallTimeGoods.getRegionId());
            goodsPool.setRegionName(mallTimeGoods.getRegionName());
            goodsPool.setIsPublish(1);
            goodsPool.setCoverImg(timeGoodsPool.getCoverImg());
            goodsPool.setDescription(timeGoodsPool.getDescription());
            mallTimeGoodsService.insertMallTimeGoods(goodsPool);
            // 删除商品池商品
            mallTimeGoodsPoolService.deleteMallTimeGoodsPoolById(mallTimeGoods.getOrderId());
            // 修改原商品
            mallTimeGoods.setPrice(timeGoodsPrice);
            mallTimeGoods.setOrderId(null);
        } else {
            AjaxResult.error("用户错误");
        }
        return toAjax(mallTimeGoodsService.updateMallTimeGoods(mallTimeGoods));
    }
}
